/**
 * Created by yangzhi on 2019/1/4.
 */


/**
 * 导出接口数据的样例
 * @return {[type]} [description]
 */
function exportApi(url,type,time) {
    layui.use(['jquery', 'excel', 'layer','util'], function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var excel = layui.excel;
        var util = layui.util;
        //订单导出
        var tab =0;
        var fileName="";
        if(url=="exportCouponList"){
            var couponId=$("#couponId").val();
            url = "/export/exportCouponList?type="+type+"&time="+time+"&couponId="+couponId;
            tab =1;
            if(type=="day") {
                fileName = time + "日优惠券数据_" + util.toDateString(new Date()) + ".xlsx";
            }else if(type=="month"){
                fileName = time + "月优惠券数据_" + util.toDateString(new Date()) + ".xlsx";
            }
        }
        // 模拟从后端接口读取需要导出的数据
        $.ajax({
            url: url
            ,dataType: 'json'
            ,success: function(res) {
                var data = res.data;
                if (res.count==0){
                    layer.alert('暂无数据可以导出');
                    return false;
                }
                // 重点！！！
                if(tab==1) {
                    data = excel.filterExportData(data, {
                        userId: 'userId',
                        userName: 'userName',
                        dispatchUserId: 'dispatchUserId',
                        dispatchUserName: 'dispatchUserName',
                        couponCodeName: 'couponCodeName',
                        createTime: function (createTime) {
                            return layui.toDateString(createTime)
                        },
                        donateTime: function (donateTime) {
                            if(donateTime>0) {
                                return layui.toDateString(donateTime)
                            }else{
                                return "------"
                            }
                        },
                        useTime: function (useTime) {
                            if(useTime>0){
                                return layui.toDateString(useTime)
                            }else{
                                return "------"
                            }
                        }
                    });
                    data.unshift({
                        userId: "领取用户ID",
                        userName: "领券用户名",
                        dispatchUserId: "派发用户ID",
                        dispatchUserName: "派发用户名",
                        couponCodeName: '券码',
                        createTime: '领券时间',
                        donateTime: '赠送时间',
                        useTime: '用券时间'
                    });
                }

                var timestart = Date.now();
                var colConf = excel.makeColConfig({
                    'F': 200,
                    'G': 200,
                    'H': 200
                }, 100);

                var rowConf = excel.makeRowConfig({
                    1: 20
                }, 30);

                excel.exportExcel(data, fileName, 'xlsx',{
                    extend: {
                        '!cols': colConf,
                        '!rows': rowConf
                    }
                });
                var timeend = Date.now();

                var spent = (timeend - timestart) / 1000;
                layer.alert('单纯导出耗时 '+spent+' s');
            }
            ,error: function() {
                layer.alert('获取数据失败，请检查是否部署在本地服务器环境下');
            }
        });
    });
}